package cn.edu.nuc.fourth.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.fourth.dao.MajorMapper;
import cn.edu.nuc.fourth.dao.SchoolMapper;
import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Major;
import cn.edu.nuc.fourth.model.School;
import cn.edu.nuc.fourth.services.interfaces.FillingService;

@Service
public class FillingServiceImpl implements FillingService {

	@Autowired
	private SchoolMapper smapper;
	@Autowired
	private MajorMapper mmapper;

	/**
	 * 办学条件填报
	 */
	@Override
	public Integer schoolAdd(School school) {

		School s = smapper.selectByPrimaryKey(school.getSid());
		if (s != null) {

			return -1;
		} else {
			Integer result = smapper.insert(school);
			return result;
		}
	}

	/**
	 * 分页办学显示列表
	 */
	@Override
	public PageInfo schoolList(Integer userid, Integer current) {

		if (current <= 0) {

			current = 1;
		}
		PageInfo pageInfo = new PageInfo(current);
		List<School> list = smapper.findByUserid(userid, pageInfo.getStart(), pageInfo.getOffset());
		Integer count = smapper.count(userid);
		// System.out.println(list);
		// System.out.println(mapper.count(userid));
		pageInfo.setCount(count);
		pageInfo.setList(list);

		return pageInfo;
	}

	/**
	 * 初始化一条办学条件记录
	 */
	@Override
	public School oneSchool(Integer sid) {

		School school = smapper.selectByPrimaryKey(sid);
		return school;
	}

	@Override
	public School schoolEdit(School school) {

		Integer result = smapper.updateByPrimaryKey(school);
		School s = smapper.selectByPrimaryKey(school.getSid());
		return s;
	}

	@Override
	public Integer schoolDelete(Integer sid) {

		Integer result = smapper.deleteByPrimaryKey(sid);
		return result;
	}

	/**
	 * 填报内容不可修改
	 */
	@Override
	public School noeditSchool(Integer sid, Integer state) {

		Integer result = smapper.noedit(sid, state);
		School school = smapper.selectByPrimaryKey(sid);
		return school;
	}

	/**
	 * 提交后的不许填报
	 */
	@Override
	public Integer noAdd(String sdate) {

		sdate = "%" + sdate + "%";
		School school = smapper.findBydate(sdate);
		if (school != null && school.getState() == 1) {

			return -1;
		}
		return 1;
	}

	///////////
	/**
	 * 专业添加
	 */
	@Override
	public Integer majorAdd(Major major) {

		Integer result = mmapper.insert(major);
		return result;
	}

	/**
	 * 专业分页
	 */
	@Override
	public PageInfo majorList(Integer userid, Integer current) {

		if (current <= 0) {

			current = 1;
		}
		PageInfo pageInfo = new PageInfo(current);
		List<Major> list = mmapper.findByUserid(userid, pageInfo.getStart(), pageInfo.getOffset());
		Integer count = mmapper.count(userid);
		pageInfo.setCount(count);
		pageInfo.setList(list);

		return pageInfo;
	}

	/**
	 * 初始化一条
	 */
	@Override
	public Major oneMajor(Integer majorid) {

		Major major = mmapper.selectByPrimaryKey(majorid);
		return major;
	}

	/**
	 * 执行修改
	 */
	@Override
	public Major majorEdit(Major major) {

		mmapper.updateByPrimaryKey(major);
		Major m = mmapper.selectByPrimaryKey(major.getMajorid());
		return m;
	}

	/**
	 * 填报后无法再填报
	 */
	@Override
	public Major noeditMajor(Integer majorid, Integer state) {

		mmapper.noedit(majorid, state);

		Major major = mmapper.selectByPrimaryKey(majorid);
		return major;
	}

	/**
	 * 删除专业
	 */
	@Override
	public Integer deleteMajor(Integer majorid) {

		Integer result = mmapper.deleteByPrimaryKey(majorid);
		return result;
	}

}
